package cn.scxy.bookcity.controller;

import cn.scxy.bookcity.pojo.dto.OrderOneAddNewDTO;
import cn.scxy.bookcity.pojo.dto.OrdersAddNewDTO;
import cn.scxy.bookcity.pojo.dto.ShoppingCarAddNewDTO;
import cn.scxy.bookcity.pojo.dto.UserLoginDTO;
import cn.scxy.bookcity.pojo.entity.CarProduct;
import cn.scxy.bookcity.pojo.entity.Orderone;
import cn.scxy.bookcity.pojo.entity.Orders;
import cn.scxy.bookcity.pojo.entity.ShoppingCart;
import cn.scxy.bookcity.pojo.vo.GoodsListItemVO;
import cn.scxy.bookcity.pojo.vo.OrderVO;
import cn.scxy.bookcity.pojo.vo.OrdersListItemVO;
import cn.scxy.bookcity.pojo.vo.shoppingCarVO;
import cn.scxy.bookcity.service.IOrderoneService;
import cn.scxy.bookcity.service.IOrdersService;
import cn.scxy.bookcity.service.IShoppingCarService;
import cn.scxy.bookcity.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order")
@Api(tags = "订单模块")
@Slf4j
public class OrderController {
    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IOrderoneService orderoneService;

    @Autowired
    private IShoppingCarService shoppingCarService;

    @ApiOperation("查询列表")
    @ApiOperationSupport(order = 430)
    @GetMapping("/orderlist/{uid}")
    public JsonResult<List<OrderVO>> list(@PathVariable Long uid) {
        log.debug("处理查询购物车请求");
        List<OrderVO> list = ordersService.list1(uid);
        return JsonResult.ok(list);
    }


    @ApiOperation("创建订单")
    @ApiOperationSupport(order =410)
    @PostMapping("/insert")
    public JsonResult<Long> insert(OrdersAddNewDTO ordersAddNewDTO){
        log.debug("接收到:{}",ordersAddNewDTO);
        Long insert = ordersService.insert(ordersAddNewDTO);
        log.debug("------111111-------{}",insert);
        return JsonResult.ok(insert);
    }

    @ApiOperation("创建Orderone订单")
    @ApiOperationSupport(order =420)
    @PostMapping("/insertOrderone")
    public JsonResult<List<OrderOneAddNewDTO>> insertOrderone(@RequestBody List<OrderOneAddNewDTO> orderOneAddNewDTO){
        log.debug("接收到:{}",orderOneAddNewDTO);
        orderoneService.insert(orderOneAddNewDTO);
        return JsonResult.ok(orderOneAddNewDTO);
    }


    @ApiOperation("删除订单信息")
    @ApiOperationSupport(order =401)
    @PostMapping("/deleteo/{oid}")
    public JsonResult<Void> deleteByoId(@PathVariable Long oid){
        log.debug("开始处理删除订单信息");
        ordersService.deleteByoId(oid);
        return JsonResult.ok();
    }

    @ApiOperation("支付订单修改状态")
    @ApiOperationSupport(order =488)
    @PostMapping("/changeState/{id}")
    public JsonResult<Void> changeState(@PathVariable Integer id){
        log.debug("开始处理删除订单信息");
        ordersService.changeState(id);
        return JsonResult.ok();
    }



    @ApiOperation("删除订单信息")
    @ApiOperationSupport(order =401)
    @PostMapping("/delete/{id}")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("开始处理删除订单信息");
        ordersService.deleteById(id);
        return JsonResult.ok();
    }


    @ApiOperation("修改订单状态为3")
    @ApiOperationSupport(order =401)
    @PostMapping("/confirm/{id}")
    public JsonResult<Void> confirm(@PathVariable Long id){
        log.debug("开始处理删除订单信息");
        ordersService.updateStateIs3ByoId(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询待付款订单信息")
    @ApiOperationSupport(order =401)
    @GetMapping("/listState0")
    public JsonResult<List<OrdersListItemVO>> listState0(HttpSession session){
        log.debug("开始处理查询待付款的订单");
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        Long uId = Long.valueOf(id);
        List<OrdersListItemVO> ordersListItemVO=ordersService.listByStateIs0(uId);
        return JsonResult.ok(ordersListItemVO);
    }

    @ApiOperation("查询待发货订单信息")
    @ApiOperationSupport(order =402)
    @GetMapping("/listState1")
    public JsonResult<List<OrdersListItemVO>> listState1(HttpSession session){
        log.debug("开始处理查询待发货的订单");
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        Long uId = Long.valueOf(id);
        List<OrdersListItemVO> ordersListItemVO=ordersService.listByStateIs1(uId);
        return JsonResult.ok(ordersListItemVO);
    }
    @ApiOperation("查询待收货订单信息")
    @ApiOperationSupport(order =403)
    @GetMapping("/listState2")
    public JsonResult<List<OrdersListItemVO>> listState2(HttpSession session){
        log.debug("开始处理查询待收货的订单");
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        Long uId = Long.valueOf(id);
        List<OrdersListItemVO> ordersListItemVO=ordersService.listByStateIs2(uId);
        return JsonResult.ok(ordersListItemVO);
    }
    @ApiOperation("查询已收货订单信息")
    @ApiOperationSupport(order =404)
    @GetMapping("/listState3")
    public JsonResult<List<OrdersListItemVO>> listState3(HttpSession session){
        log.debug("开始处理查询已收货的订单");
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        Long uId = Long.valueOf(id);
        List<OrdersListItemVO> ordersListItemVO=ordersService.listByStateIs3(uId);
        return JsonResult.ok(ordersListItemVO);
    }


    @ApiOperation("查询当前登录用户")
    @ApiOperationSupport(order = 305)
    @GetMapping("/getUserLogin")
    public JsonResult<UserLoginDTO> getUserLogin(HttpSession session){
        log.debug("查询当前登录用户");
        UserLoginDTO user = (UserLoginDTO) session.getAttribute("user");
        log.debug("获取到的值:{}",user);
        return JsonResult.ok(user);
    }


}
